import pandas as pd
import statsmodels.api as sm
import map


# 加载数据
# df = pd.read_excel(r"C:\Users\lizhiyi.i\PycharmProjects\BiShe\Datas\安徽省.xlsx")
# df = pd.read_excel(r"C:\Users\14774\Desktop\毕业设计\数据集\整理后\安徽省.xlsx")


def getData(province):
    df = read_province_excel(province)
    # 提取自变量和因变量
    x = df[['地方财政科学技术支出(亿元)']]
    y1 = df['规模以上工业企业R&D人员全时当量(人年)']
    y2 = df['规模以上工业企业R&D项目数(项)']
    y3 = df['规模以上工业企业新产品项目数(项)']
    y4 = df['规模以上工业企业专利申请数(件)']
    y5 = df['国内专利申请受理量(项)']
    y6 = df['国内专利申请授权量(项)']

    # 创建多元回归模型并拟合
    model_y1 = sm.OLS(y1, sm.add_constant(x)).fit()
    model_y2 = sm.OLS(y2, sm.add_constant(x)).fit()
    model_y3 = sm.OLS(y3, sm.add_constant(x)).fit()
    model_y4 = sm.OLS(y4, sm.add_constant(x)).fit()
    model_y5 = sm.OLS(y5, sm.add_constant(x)).fit()
    model_y6 = sm.OLS(y6, sm.add_constant(x)).fit()

    # data = [model_y1.rsquared_adj, model_y2.rsquared_adj, model_y3.rsquared_adj, model_y4.rsquared_adj,
    #         model_y5.rsquared_adj, model_y6.rsquared_adj]
    data = [model_y1.rsquared, model_y2.rsquared, model_y3.rsquared, model_y4.rsquared, model_y5.rsquared,
            model_y6.rsquared]
    return data


def read_province_excel(province):
    # file_path = r"C:\Users\lizhiyi.i\PycharmProjects\BiShe\Datas\安徽省.xlsx"
    file_path=r"D:\Python\vippython\chap2\Datas\安徽省.xlsx"
    file_path = file_path.replace("安徽省", province)
    df = pd.read_excel(file_path)
    return df


def main():
    provinces = ["安徽省", "北京市", "福建省", "甘肃省", "广东省", "广西壮族自治区", "贵州省", "海南省", "河北省",
                 "河南省", "黑龙江省", "湖北省", "湖南省", "吉林省", "江苏省", "江西省", "辽宁省", "内蒙古自治区",
                 "宁夏回族自治区", "青海省", "山东省", "山西省", "陕西省", "上海市", "四川省", "天津市", "西藏自治区",
                 "新疆维吾尔自治区", "云南省", "浙江省", "重庆市"]
    provinces_rsquared = [[None]*6]*31
    for i in range(31):
        print(i)
        provinces_rsquared[i] = getData(provinces[i])

    # 输出所有省份的数据结果：
    # for i in range(31):
    #     print(provinces[i] + ":")
    #     for j in range(len(provinces_rsquared[i])):
    #         print("  " + str(provinces_rsquared[i][j]))

    map.getMap(provinces, provinces_rsquared)


if __name__ == '__main__':
    main()
